#ifndef YHAIDA_THREADPOOL_H
#define YHAIDA_THREADPOOL_H
#include "Thread.h"
#include "../base/Utils.h"
#include "../mutex/MutexLock.h"
#include "../mutex/Condition.h"
#include <functional>
#include <deque>
#include <vector>
#include <assert.h>
#include <string>
#include <algorithm>
namespace yhaida
{
    class ThreadPool : Utils::nocapyable
    {
    public:
        typedef std::function<void()> TaskFunc;

        explicit ThreadPool(const std::string &name=std::string());
        ~ ThreadPool();

        void start(int numThreads);
        void stop();
        //生产者函数
        void appendTask(TaskFunc);

        void setThreadInitCallback(const TaskFunc& cb)
        { _threadInitCallback = cb; }

    private:
        //线程的工作函数
        void runInThread();
        //消费者函数
        TaskFunc getTask();

        MutexLock _mutex;
        Condition _cv;
        std::deque<TaskFunc> _queue;
        std::vector<std::unique_ptr<Thread>> _threads;
        bool _start;
        std::string _name;

        TaskFunc _threadInitCallback;

    };
}

#endif